/**
 * JsonSerialize.java
 * Created at 2016年4月16日
 * Created by Jackie Liu
 * Copyright (C) 2016 Jackie Liu, All rights reserved.
 */
package com.huabo.serialize;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * <p>ClassName: JsonSerialize</p>
 * <p>Description: json序列化实现</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年4月16日</p>
 */
public class FastJsonSerializer implements Serializer {
	
	/* (non-Javadoc)
	 * <p>Title: getString</p>
	 * <p>Description: </p>
	 * @param object
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getString(java.lang.Object)
	 */
	@Override
	public String getString(Object object) {
		if (object == null) {
			throw new NullPointerException();
		}
		return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
	}

	/* (non-Javadoc)
	 * <p>Title: getByteArray</p>
	 * <p>Description: </p>
	 * @param object
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getByteArray(java.lang.Object)
	 */
	@Override
	public byte[] getByteArray(Object object) {
		if (object == null) {
			throw new NullPointerException();
		}
		return JSON.toJSONBytes(object, SerializerFeature.WriteMapNullValue);
	}

	/* (non-Javadoc)
	 * <p>Title: getObject</p>
	 * <p>Description: </p>
	 * @param serializeText
	 * @param clazz
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getObject(java.lang.String, java.lang.Class)
	 */
	@Override
	public <T> T getObject(String serializeText, Class<T> clazz) {
		if (serializeText == null || "".equals(serializeText)) {
			throw new NullPointerException();
		}
		if (clazz == null) {
			throw new NullPointerException();
		}
		return JSON.parseObject(serializeText, clazz);
	}

	/* (non-Javadoc)
	 * <p>Title: getObject</p>
	 * <p>Description: </p>
	 * @param serializeByteArray
	 * @param clazz
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getObject(byte[], java.lang.Class)
	 */
	@Override
	public <T> T getObject(byte[] serializeByteArray, Class<T> clazz) {
		if (serializeByteArray == null || serializeByteArray.length <= 0) {
			throw new NullPointerException();
		}
		if (clazz == null) {
			throw new NullPointerException();
		}
		return JSON.parseObject(serializeByteArray, clazz);
	}
}
